module EasyForm
  module Helpers
    module Required
      private

      def required_field?
        @required
      end

      def calculate_required
        return options[:required] if !options[:required].nil?
        if has_validators?
          required_by_validators?
        else
          EasyForm.required_by_default
        end
      end

      def required_by_validators?
        (attribute_validators + reflection_validators).any? {|v| v.kind == :presence && valid_validator?(v)}
      end

      def required_class
        required_field? ? :required : :optional
      end
    end
  end
end